/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * Utils class that provides methods related to manipulating time.
 * 
 * @author Kyle O'Connor
 * 
 */
public class TimeUtils {

	public static long ONE_HOUR_IN_MS = 3600000;
	public static long TWO_HOURS_IN_MS = 7200000;
	public static long SIX_HOURS_IN_MS = 21600000;
	public static long ONE_MIN_IN_MS = 60000;
	public static long FIVE_MIN_IN_MS = 300000;
	public static long THIRTY_SEC_IN_MS = 30000;
	public static long ONE_SEC_IN_MS = 1000;

	/**
	 * Utils class can not be instantiated.
	 */
	private TimeUtils() { }

	/**
	 * Queries if the current date is in daylight savings time in this time
	 * zone.
	 * 
	 * @return true if the current date is in daylight savings time, false,
	 *         otherwise.
	 */
	public static boolean isDaylightSavingsTime() {
		return TimeZone.getDefault().inDaylightTime(new Date());
	}

	/**
	 * Calculates the difference between the current date and the given date.
	 * 
	 * @param date
	 *            the given <code>Data<code>
	 * @param X
	 *            specified number of hours ago
	 * @return true if the difference is greater than X hours, false, otherwise.
	 */
	public static boolean isOverXHoursAgo(Date date, int X) {
		Date now = new Date();

		long diff = now.getTime() - date.getTime();
		long hoursInMS = X * (60 * 60 * 1000);
		
		if (diff > hoursInMS) {
			return true;
		}

		return false;
	}

	/**
	 * Calculates the time zone offset based on the current time and also takes
	 * into account if daylight savings time is in effect.
	 * 
	 * @return the time zone offset in milliseconds as <code>long</code>
	 */
	public static long calculateTimeZoneOffset() {
		Calendar calendar = new GregorianCalendar();
		long tzOffset = calendar.get(Calendar.ZONE_OFFSET);
		long dstOffset = calendar.get(Calendar.DST_OFFSET);

		return tzOffset + dstOffset;
	}
	
	public static int convertSecondsForSP(int seconds) {
		return seconds * 100;
	}

	public static int convertSecondsForST(int seconds) {
		return seconds * 1000;
	}

	public static void main(String[] args) {	
		// testing dates and DST
		Calendar calendar = new GregorianCalendar();
		System.out.println("DATE: " + calendar.get(Calendar.DATE));
		System.out.println(calendar.getTimeZone().inDaylightTime(calendar.getTime()));
		System.out.println("ZONE_OFFSET_HR: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
		System.out.println("DST_OFFSET_HR: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
		System.out.println("ZONE_OFFSET_MS: " + (calendar.get(Calendar.ZONE_OFFSET)));
		System.out.println("DST_OFFSET_MS: " + (calendar.get(Calendar.DST_OFFSET)));
		System.out.println(calculateTimeZoneOffset());

		Calendar calendar2 = new GregorianCalendar(2010, 0, 0);
		System.out.println(calendar2.getTimeZone().inDaylightTime(calendar2.getTime()));
		System.out.println("DATE: " + calendar2.get(Calendar.DATE));
		System.out.println("ZONE_OFFSET_HR: " + (calendar2.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
		System.out.println("DST_OFFSET_HR: " + (calendar2.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
		System.out.println("ZONE_OFFSET_MS: " + (calendar2.get(Calendar.ZONE_OFFSET)));
		System.out.println("DST_OFFSET_MS: " + (calendar2.get(Calendar.DST_OFFSET)));
	}

}
